package com.bdsoft.actions;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestSecurityController {

    @GetMapping("/security")
    public Map<String, Object> testSecurity() {
        Map<String, Object> result = new HashMap<>();
        
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated()) {
            result.put("authenticated", true);
            result.put("username", authentication.getName());
            result.put("authorities", authentication.getAuthorities());
        } else {
            result.put("authenticated", false);
        }
        
        result.put("message", "Spring Security集成测试成功");
        return result;
    }

    @GetMapping("/public")
    public Map<String, Object> publicEndpoint() {
        Map<String, Object> result = new HashMap<>();
        result.put("message", "这是一个公开接口，无需认证");
        return result;
    }
} 